/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package amfservices.actions;

import config.PGConfig;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import pgentity.Cote;
import pgentity.EntityContext;
import pgentity.Gift;
import pgentity.Penguin;
import pgentity.User;
import pgentity.events.release_event.ReleaseEventServices;
import share.AMFBuilder;
import share.PGError;
import share.PGException;
import share.PGMacro;

/**
 *
 * @author Salm
 */
class ReleaseEvent {
    private ReleaseEvent() {}
    public static final ReleaseEvent INST = new ReleaseEvent();
    
    public Object pickReleaseEventItems(String uid, String coteID,
            Map<String, Number> items, long now)
    {
        checkEventTime(now);
        
        User user = User.getUser(uid);
        Cote cote = Cote.getCote(uid, coteID);
        
        return ReleaseEventServices.SERVICES.pickReleaseEventItems(user, cote, items);
    }
    
    public Object penguinSpawnReleaseEventItem(String uid, String coteID,
            Collection<String> penguinIDs, long now)
    {
        checkEventTime(now);
        
        Cote cote = Cote.getCote(uid, coteID);
        Collection<Penguin> penguins = Penguin
                .getPenguins(uid, coteID, penguinIDs);
        
        return ReleaseEventServices.SERVICES
                .spawnReleaseEventItems(cote, penguins, now);
    }
    
    public Object returnReleaseEvent(String uid, String eventID, long now)
    {
        checkEventTime(now);
        
        EntityContext context = EntityContext.getContext(uid);
        Object resp = ReleaseEventServices.SERVICES
                .returnReleaseEvent(context, eventID, now);
        
        context.saveToDB();
        
        return AMFBuilder.make(PGMacro.PRIZE, resp);
    }
    
    public Object receiveTotalEvent(String uid, long now)
    {
        checkEventTime(now);
        
        EntityContext context = EntityContext.getContext(uid);
        Map<Integer, Gift> gifts = ReleaseEventServices.SERVICES
                .receiveTotalEvent(context, now);
        
        context.saveToDB();
        
        Map<Integer, Object> giftAMFs = new HashMap(gifts.size());
        for (Map.Entry<Integer, Gift> giftData : gifts.entrySet()) {
            Integer index = giftData.getKey();
            Gift gift = giftData.getValue();
            
            giftAMFs.put(index, gift.buildIDAMF());
        }
        
        return AMFBuilder.make(PGMacro.GIFTS, giftAMFs);
    }
    
    public Object buyREItem(String uid, String itemID, String payType, int nItem, long now)
    {
        checkEventTime(now);
        
        PGException.Assert(nItem > 0, PGError.INVALID_REITEM,
                "NoItem must be greater than 0");
        
        User user = User.getUser(uid);
        ReleaseEventServices.SERVICES.buyREItem(user, itemID, payType, nItem, now);
        user.saveToDB();
        
        return Collections.EMPTY_MAP;
    }
    
    private void checkEventTime(long now)
    {
        PGException.Assert(PGConfig.inst().releaseEvent().inEvent(now),
                PGError.EVENT_IS_ENDED, "Event is ended or not started");
    }
}
